package com.jdzy.student.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdzy.model.student.dtos.JobPostingsDto;
import com.jdzy.model.student.pojos.JobPostings;
import com.jdzy.student.mapper.JobPostingsMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import com.jdzy.student.service.JobPostingsService;

import java.util.List;

@Service
@RequiredArgsConstructor
public class JobPostingsServiceImpl extends ServiceImpl<JobPostingsMapper, JobPostings> implements JobPostingsService {

    private final JobPostingsMapper jobPostingsMapper;

    /**
     * 获取所有的岗位信息----按最新岗位的时间---根据个人优势
     * @param current
     * @param size
     * @return
     */
    @Override
    public IPage<JobPostings> getJobPostingsPage(long current, long size) {
        LambdaQueryWrapper<JobPostings> queryWrapper = new LambdaQueryWrapper<>();
        // 可以在这里添加任何需要的查询条件
        // queryWrapper.eq(JobPostings::getSomeField, someValue);
        // 根据创建时间降序排序
        queryWrapper.orderByDesc(JobPostings::getCreateTime);
        return jobPostingsMapper.selectPage(new Page<>(current, size), queryWrapper);
    }

    /**
     * 保存或修改岗位信息
     * @param jobPostingsDto
     */
    @Override
    public void saveOrUpdateJob(JobPostingsDto jobPostingsDto) {
        // 判断是否存在 ID，如果存在则是更新，否则是保存
        if (jobPostingsDto.getId() != null) {
            // 更新操作：使用 Lambda 表达式根据 ID 更新记录
            UpdateWrapper<JobPostings> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", jobPostingsDto.getId())
                    .set("job_name", jobPostingsDto.getJobName())
                    .set("job_type", jobPostingsDto.getJobType())
                    .set("job_description", jobPostingsDto.getJobDescription())
                    .set("address", jobPostingsDto.getAddress())
                    .set("job_link", jobPostingsDto.getJobLink())
                    .set("company", jobPostingsDto.getCompany());
            jobPostingsMapper.update(null, updateWrapper);
        } else {
            // 保存操作：将 Dto 转换为实体类并保存
            JobPostings jobPostings = new JobPostings();
            BeanUtils.copyProperties(jobPostingsDto, jobPostings);  // 使用 Spring 提供的工具类进行属性复制
            jobPostingsMapper.insert(jobPostings);
        }
    }
}
